/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.ui.draggable;

import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.ajax.AbstractDefaultAjaxBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.IAjaxCallDecorator;
import org.apache.wicket.ajax.calldecorator.CancelEventIfNoAjaxDecorator;

import com.noocodecommit.wicket.stuff.jquery.exception.CannotApplyJQBehaviorException;
import com.noocodecommit.wicket.stuff.jquery.plugin.IJQPluginComponent;
import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * Ajax behavior for drag start handle
 * 
 * @author Benoit Bouchez
 * 
 */
public abstract class OnDragStartBehavior extends AbstractDefaultAjaxBehavior
{
	protected void respond(AjaxRequestTarget target)
	{
//		Request req = RequestCycle.get().getRequest();
//		((JQDraggable) getComponent()).setHelperPositionTop(Integer.parseInt(req.getParameter("top")));
//		((JQDraggable) getComponent()).setHelperPositionLeft(Integer.parseInt(req.getParameter("left")));
		this.onStart(target);
	}

	protected CharSequence getCallbackScript(boolean onlyTargetActivePage)
	{
//		return generateCallbackScript("wicketAjaxGet('" + getCallbackUrl(onlyTargetActivePage) + "&top=' + $(ui.originalPosition.top)[0] + '" + "&left=' + $(ui.originalPosition.left)[0] + '" + "'");
		return generateCallbackScript("wicketAjaxGet('" + getCallbackUrl(onlyTargetActivePage) + "'");
	}

	@Override
	protected IAjaxCallDecorator getAjaxCallDecorator()
	{
		return new CancelEventIfNoAjaxDecorator(super.getAjaxCallDecorator());
	}

	@Override
	protected void onBind()
	{
		// Wicket Binding
		super.onBind();
		// Check component binding
		if(!this.getComponent().getClass().equals(JQDraggable.class))
		{
			throw new CannotApplyJQBehaviorException(OnDragStartBehavior.class, this.getComponent().getClass(), JQDraggable.class);
		}
		// Plugin ajax support & options clone export only if PLUGIN_INIT_ONCE
		JQDraggable component = (JQDraggable)this.getComponent();
		if(component.getPlugin().getInitMode() == JQPlugin.PLUGIN_INIT_ONCE)
		{
			if (component.getPlugin().attachComponentForDocumentReadySupportingAjax((IJQPluginComponent) getComponent()))
			{
				try
				{
					component.setOptions((JQDraggableOptions) component.getPlugin().getOptions().clone());
				}
				catch (CloneNotSupportedException e)
				{
					throw new WicketRuntimeException(e);
				}
			}
		}
		// JQuery binding to callback function
		((JQDraggable) getComponent()).getOptions().setStartScript(((JQDraggable) getComponent()).getOptions().getStartScript() + this.getCallbackScript(false).toString());
	}

	/**
	 * Called when a drag start event is fired
	 * 
	 * @param target
	 */
	public abstract void onStart(AjaxRequestTarget target);
}
